package com.yao.guava_demo.optional;

import com.google.common.base.Optional;

public class Demo1 {
    public static void main(String[] args) {
        //Optional.of 返回要参数的Optional类的实例。检查传递的值是否为null
        Optional<Integer> a = Optional.of(new Integer(10));
        //直接传入null会抛出异常
//        Optional<Integer> b=Optional.of(null);
        Integer c = null;
        //传入null引用也会抛出异常
//        Exception in thread "main" java.lang.NullPointerException
//        at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:877)
//        at com.google.common.base.Optional.of(Optional.java:103)
//        at com.yao.guava_demo.optional.OptionalDemo.main(OptionalDemo.java:12)
//        Optional<Integer> o = Optional.of(c);

        Optional<Integer> d=Optional.absent();
        sum(a, d);
        System.out.println("exit=================");
    }

    public static Integer sum(Optional<Integer> a, Optional<Integer> b) {
        return a.get() + b.get();
    }


}
